package com.yanqu.road.server.cmd.unioncampwar;

import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.pb.unionCampWar.UnionCampWar;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.unioncampwar.UnionCampWarModule;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;


@Cmd(code = GameProtocol.S_UNION_CAMP_WAR_ATTACK_TEAM, desc = "商帮进攻")
public class UnionCampWarAttackCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (null == player) {
            return;
        }
        UnionCampWar.UnionCampWarAttackReqMsg reqMsg = UnionCampWar.UnionCampWarAttackReqMsg.parseFrom(packet.getBytes());

        List<PatronsProto.PatronsSimpleTempMsg> patronsBattleInfoList = new ArrayList<>();
        ret = player.getModule(UnionCampWarModule.class).checkAttackRet(reqMsg.getPatronsIdList(), reqMsg.getBuffId(), patronsBattleInfoList, reqMsg.getAttackType());
        if (0 != ret) {
            return;
        }

        UnionCampWar.CrossUnionCampWarAttackReqMsg.Builder crossReq = UnionCampWar.CrossUnionCampWarAttackReqMsg.newBuilder();
        crossReq.setClientReq(reqMsg);
        crossReq.addAllPatronsInfo(patronsBattleInfoList);
        crossReq.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        crossReq.setNowAttackTimes(player.getModule(UnionCampWarModule.class).getAttackTimes());
        player.sendPacket(CrossProtocol.C3_UNION_CAMP_WAR_ATTACK_TEAM, crossReq);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (null == player) {
            return;
        }
        if (0 != ret) {
            player.sendPacket(ClientProtocol.U_UNION_CAMP_WAR_ATTACK_TEAM, UnionCampWar.UnionCampWarAttackRespMsg.newBuilder().setRet(ret));
        }

    }
}
